/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.minierp.bean.OrderParameterCarrier;
import com.minierp.bean.QueryParameterCarrier;
import com.minierp.bean.QueryResult;
import com.minierp.bean.UserBean;
import com.minierp.dao.EstablishmentCommunicateDao;
import com.minierp.dao.EstablishmentContactDao;
import com.minierp.dao.EstablishmentLevelDao;
import com.minierp.dao.EstablishmentTypeDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.PhoneBookDao;
import com.minierp.dao.QueryDao;
import com.minierp.dao.SmsAlertDao;
import com.minierp.dao.SmsCountDao;
import com.minierp.dao.StafferDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.EstablishmentCommunicate;
import com.minierp.model.EstablishmentContact;
import com.minierp.model.FeeType;
import com.minierp.model.SmsAlert;
import com.minierp.model.SmsAlertReceiver;
import com.minierp.model.SmsCount;
import com.minierp.model.SmsTask;
import com.minierp.model.Staffer;
import com.minierp.model.TallySet;
import com.minierp.model.User;
import com.minierp.service.SmsService;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class SmsMACtr extends MultiActionController {
	private StafferDao stafferDao;
	private EstablishmentTypeDao estTypeDao;
	private EstablishmentLevelDao estLevelDao;
	private SmsCountDao smsCountDao;
	private SmsAlertDao smsAlertDao;
	private EstablishmentContactDao contactDao;
	private EstablishmentCommunicateDao establishmentCommunicateDao;
	private PhoneBookDao phoneBookDao;
	private QueryDao queryDao;
	private IdGenerator idGenerator;
	private SmsService smsService;
	private String sendFormView;
	private String sendFormMultiView;
	private String myHistoryListView;
	private String historyListView;
	private String historyDetailView;
	private String subscribeView;
	private String receiverListView;

	public ModelAndView handleSendForm(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.sendFormView);
		String type = request.getParameter("type");
		if (type != null && type.equals("MULTI")) {
			if (!EncCommonHelper.checkPrivilege(request, "SMS_SEND")) {
				throw new PermissionDennyException("Permission Denny");
			}

			mv = new ModelAndView(this.sendFormMultiView);
			mv.addObject("estTypeList", this.estTypeDao.getList(true));
			mv.addObject("estLevelList", this.estLevelDao.getList(true));
		} else {
			if (!EncCommonHelper.checkPrivilege(request, "SMS_SEND_SIMP")) {
				throw new PermissionDennyException("Permission Denny");
			}

			UserBean ent = EncCommonHelper.getCurrentUserAccount(request);
			mv.addObject("phoneBookList",
					this.phoneBookDao.getList(ent.getUser().getStafferId()));
		}

		mv.addObject("type", type);
		mv.addObject("stafferList", this.stafferDao.getSmsList());
		EnterpriseInfo arg14 = EncCommonHelper.getEnterpriseInfo(request);
		if (arg14 != null) {
			mv.addObject("sign", arg14.getShortName());
		}

		String estIdStr = request.getParameter("est_id_str");
		String estCommId = request.getParameter("comm_id");
		String mobile = request.getParameter("mobile");
		List minuteSf;
		if (estIdStr != null && !estIdStr.equals("")) {
			String[] arg15 = estIdStr.split(";");
			long[] arg17 = this.parseIds(arg15);
			minuteSf = this.contactDao.getEstSmsList(arg17);
			String arg20 = this.parseContactReceiver(minuteSf);
			mv.addObject("receiver", arg20);
		} else if (estCommId != null && !estCommId.equals("")) {
			EstablishmentCommunicate dateSf = (EstablishmentCommunicate) this.stafferDao
					.get(EstablishmentCommunicate.class,
							Long.parseLong(estCommId));
			if (dateSf != null && dateSf.getCommType().equals("PLAN")) {
				String hourSf = "";
				int now;
				if (dateSf.getForAll().equals("Y")) {
					minuteSf = this.stafferDao.getSmsList();

					for (now = 0; now < minuteSf.size(); ++now) {
						Staffer arg21 = (Staffer) minuteSf.get(now);
						hourSf = this.convertReceiverStr(hourSf,
								arg21.getMobile(), arg21.getStafferName());
					}
				} else {
					minuteSf = this.establishmentCommunicateDao
							.getReceiverList(dateSf.getId());

					for (now = 0; now < minuteSf.size(); ++now) {
						Object[] objs = (Object[]) minuteSf.get(now);
						Staffer s = (Staffer) objs[1];
						hourSf = this.convertReceiverStr(hourSf, s.getMobile(),
								s.getStafferName());
					}
				}

				mv.addObject("receiver", hourSf);
				mv.addObject("sign", dateSf.getCreaterName());
			}
		} else if (mobile != null && !mobile.equals("")) {
			mv.addObject("receiver",
					mobile + "<" + request.getParameter("rec_name") + ">");
		} else {
			mv.addObject("receiver", request.getParameter("receiver"));
		}

		mv.addObject("content", request.getParameter("content"));
		SimpleDateFormat arg16 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat arg18 = new SimpleDateFormat("HH");
		SimpleDateFormat arg19 = new SimpleDateFormat("mm");
		Date arg22 = new Date();
		mv.addObject("planTimeDate", arg16.format(arg22));
		mv.addObject("planTimeHour", arg18.format(arg22));
		mv.addObject("planTimeMinute", arg19.format(arg22));
		mv.addObject("smsSignFormat",
				EncCommonHelper.getProperty("smsSignFormat"));
		return mv;
	}

	private long[] parseIds(String[] arr) {
		long[] idArr = new long[arr.length];

		for (int i = 0; i < idArr.length; ++i) {
			if (arr[i] != null && !arr[i].trim().equals("")) {
				idArr[i] = Long.parseLong(arr[i]);
			} else {
				idArr[i] = 0L;
			}
		}

		return idArr;
	}

	private String parseContactReceiver(List contactList) {
		String receiver = "";

		for (int i = 0; i < contactList.size(); ++i) {
			Object[] objs = (Object[]) contactList.get(i);
			EstablishmentContact c = (EstablishmentContact) objs[1];
			receiver = this.convertReceiverStr(receiver, c.getMobile(),
					c.getContactName());
		}

		return receiver;
	}

	private String convertReceiverStr(String receiverStr, String mobile,
			String name) {
		String str = receiverStr;
		if (receiverStr == null) {
			str = "";
		}

		if (PageHelper.isMobileCode(mobile)) {
			if (!str.equals("")) {
				str = str + "\n";
			}

			str = str + mobile + "<" + name + ">";
		}

		return str;
	}

	public ModelAndView handleSend(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "SMS_SEND_SIMP")
				&& !EncCommonHelper.checkPrivilege(request, "SMS_SEND")) {
			throw new Exception("Permission deny!");
		} else {
			String content = request.getParameter("content");
			String receiver = request.getParameter("receiver");
			String planTimeDate = request.getParameter("planTimeDate");
			String planTimeHour = request.getParameter("planTimeHour");
			String planTimeMinute = request.getParameter("planTimeMinute");
			String sign = request.getParameter("sign");
			String planTimeCb = request.getParameter("planTimeCb");
			String signCb = request.getParameter("signCb");
			String type = request.getParameter("type");
			if (content != null && signCb != null && signCb.equals("1")) {
				content = PageHelper.createSmsContentWithSign(content, sign);
			}

			ModelAndView mv = new ModelAndView(new RedirectView(
					"/sms/SmsSendForm.ihtm?type=" + type));
			String msg = "";
			Date planTime = null;
			if (planTimeCb != null && planTimeCb.equals("1")) {
				String ub = planTimeDate + " " + planTimeHour + ":"
						+ planTimeMinute;
				SimpleDateFormat recArr = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm");
				planTime = recArr.parse(ub);
			}

			UserBean arg22 = EncCommonHelper.getCurrentUserAccount(request);
			String[] arg23 = receiver.split("\n");
			ArrayList existArr = new ArrayList();

			for (int i = 0; i < arg23.length; ++i) {
				if (arg23[i] != null && !arg23[i].equals("")) {
					String[] tmpArr = arg23[i].split("<");
					String recCode = "";
					String recName = "";
					if (tmpArr.length > 0 && tmpArr[0] != null) {
						recCode = tmpArr[0].trim();
					}

					if (tmpArr.length > 1 && tmpArr[1] != null) {
						recName = tmpArr[1].trim();
						int index = recName.indexOf(">");
						if (index != -1) {
							recName = recName.substring(0, index);
						}
					}

					if (!recCode.equals("") && !existArr.contains(recCode)) {
						this.saveSmsTask(content, planTime, recCode, recName,
								arg22);
						existArr.add(recCode);
					}
				}
			}

			msg = "OK";
			EncCommonHelper.setAlertMessage(request, msg);
			return mv;
		}
	}

	private void saveSmsTask(String content, Date planTime, String recCode,
			String recName, UserBean ub) {
		SmsTask t = new SmsTask();
		t.setContent(content);
		t.setCreaterId(ub.getStaffer().getId());
		t.setCreaterName(ub.getStaffer().getStafferName());
		t.setCreateTime(new Date());
		t.setId(this.idGenerator.getId(SmsTask.class.getName()));
		t.setPlanTime(planTime);
		t.setReceiverCode(recCode);
		t.setReceiverName(recName);
		this.stafferDao.save(t);
	}

	public ModelAndView handleSendMyself(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String result = "";
		String mobile = ub.getStaffer().getMobile();
		if (!PageHelper.isMobileCode(mobile)) {
			result = "NO_MOBILE";
		} else {
			String out = request.getParameter("content");
			String sign = request.getParameter("sign");
			if (out != null && sign != null && !sign.trim().equals("")) {
				out = PageHelper.createSmsContentWithSign(out, sign);
			}

			this.saveSmsTask(out, (Date) null, mobile, ub.getStaffer()
					.getStafferName(), ub);
			result = "OK";
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ServletOutputStream out1 = response.getOutputStream();
		out1.write(result.getBytes());
		out1.flush();
		out1.close();
		return null;
	}

	public ModelAndView handleJsonTest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ArrayList arr = new ArrayList();
		User u = new User();
		u.setId(0L);
		u.setUserName("lpf");
		u.setPassword("123");
		ArrayList tsArr = new ArrayList();
		TallySet ts = new TallySet();
		ts.setId(0L);
		ts.setTitle("ts1");
		tsArr.add(ts);
		ts = new TallySet();
		ts.setId(1L);
		ts.setTitle("ts2");
		tsArr.add(ts);
		UserBean ub = new UserBean();
		ub.setUser(u);
		ub.setDepName("dep name");
		ub.setTallySets(tsArr);
		arr.add(ub);
		u = new User();
		u.setId(1L);
		u.setUserName("paul\"\\\'林");
		u.setPassword("123");
		arr.add(u);
		HashMap map = new HashMap();
		map.put("key\"1", "value1");
		map.put("key2", "value2");
		FeeType t = new FeeType();
		t.setId(0L);
		t.setTypeName("fee type 1");
		map.put("feeType", t);
		arr.add(map);
		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ServletOutputStream out = response.getOutputStream();
		ObjectMapper mapper = new ObjectMapper();
		mapper.writeValue(out, arr);
		out.flush();
		out.close();
		return null;
	}

	public ModelAndView handleMyHistoryList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.myHistoryListView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String hisObjName = "com.minierp.model.SmsHistory";
		String hisObjShortName = "h";
		HashMap shortNameMap = new HashMap();
		String[] retObjs = new String[]{hisObjShortName};
		Vector qpVe = new Vector();
		HashMap parameters = new HashMap();
		shortNameMap.put(hisObjName, hisObjShortName);
		QueryParameterCarrier qp = new QueryParameterCarrier();
		qp.objectName = hisObjName;
		qp.propertyName = "createrId";
		qp.value = " h.createrId=" + ub.getStaffer().getId();
		qp.dataType = 6;
		qp.logicType = 10;
		qpVe.add(qp);
		String searchType = request.getParameter("search_type");
		String startDate = request.getParameter("start_date");
		String endDate = request.getParameter("end_date");
		String status = request.getParameter("status");
		String recName = request.getParameter("rec_name");
		String recCode = request.getParameter("rec_code");
		if (searchType != null && !searchType.equals("")) {
			if (searchType.equals("2")) {
				qp = new QueryParameterCarrier();
				qp.objectName = hisObjName;
				qp.propertyName = "createTime";
				qp.value = startDate;
				qp.dataType = 4;
				qp.logicType = 5;
				qpVe.add(qp);
				qp = new QueryParameterCarrier();
				qp.objectName = hisObjName;
				qp.propertyName = "createTime";
				qp.value = EncCommonHelper
						.getNextDateStr(endDate, "yyyy-MM-dd");
				qp.dataType = 4;
				qp.logicType = 4;
				qpVe.add(qp);
			} else {
				String arg26 = request.getParameter("cb_cre_date");
				String arg28 = request.getParameter("cb_status");
				String oParas = request.getParameter("cb_rec_name");
				String qr = request.getParameter("cb_rec_code");
				if (arg26 != null && arg26.equals("1")) {
					qp = new QueryParameterCarrier();
					qp.objectName = hisObjName;
					qp.propertyName = "createTime";
					qp.value = startDate;
					qp.dataType = 4;
					qp.logicType = 5;
					qpVe.add(qp);
					qp = new QueryParameterCarrier();
					qp.objectName = hisObjName;
					qp.propertyName = "createTime";
					qp.value = EncCommonHelper.getNextDateStr(endDate,
							"yyyy-MM-dd");
					qp.dataType = 4;
					qp.logicType = 4;
					qpVe.add(qp);
					parameters.put("cbCreDate", arg26);
				}

				if (arg28 != null && arg28.equals("1")) {
					if (!status.equalsIgnoreCase("A")) {
						qp = new QueryParameterCarrier();
						qp.objectName = hisObjName;
						qp.propertyName = "status";
						qp.value = status;
						qp.dataType = 1;
						qp.logicType = 1;
						qpVe.add(qp);
					}

					parameters.put("cbStatus", arg28);
				}

				if (oParas != null && oParas.equals("1")) {
					qp = new QueryParameterCarrier();
					qp.objectName = hisObjName;
					qp.propertyName = "receiverName";
					qp.value = recName;
					qp.dataType = 1;
					qp.logicType = 9;
					qpVe.add(qp);
					parameters.put("cbRecName", oParas);
				}

				if (qr != null && qr.equals("1")) {
					qp = new QueryParameterCarrier();
					qp.objectName = hisObjName;
					qp.propertyName = "receiverCode";
					qp.value = recCode;
					qp.dataType = 1;
					qp.logicType = 9;
					qpVe.add(qp);
					parameters.put("cbRecCode", qr);
				}
			}
		} else {
			searchType = "2";
			Date qParas = new Date();
			SimpleDateFormat p1 = new SimpleDateFormat("yyyy-MM");
			startDate = p1.format(qParas) + "-01";
			p1 = new SimpleDateFormat("yyyy-MM-dd");
			endDate = p1.format(qParas);
			qp = new QueryParameterCarrier();
			qp.objectName = hisObjName;
			qp.propertyName = "createTime";
			qp.value = startDate;
			qp.dataType = 4;
			qp.logicType = 5;
			qpVe.add(qp);
			qp = new QueryParameterCarrier();
			qp.objectName = hisObjName;
			qp.propertyName = "createTime";
			qp.value = EncCommonHelper.getNextDateStr(endDate, "yyyy-MM-dd");
			qp.dataType = 4;
			qp.logicType = 4;
			qpVe.add(qp);
		}

		parameters.put("searchType", searchType);
		parameters.put("startDate", startDate);
		parameters.put("endDate", endDate);
		parameters.put("status", status);
		parameters.put("recName", recName);
		parameters.put("recCode", recCode);
		QueryParameterCarrier[] arg27 = new QueryParameterCarrier[qpVe.size()];

		for (int arg29 = 0; arg29 < qpVe.size(); ++arg29) {
			arg27[arg29] = (QueryParameterCarrier) qpVe.get(arg29);
		}

		OrderParameterCarrier arg31 = new OrderParameterCarrier();
		arg31.objectName = hisObjName;
		arg31.propertyName = "id";
		arg31.orderTarget = "desc";
		OrderParameterCarrier[] arg30 = new OrderParameterCarrier[]{arg31};
		QueryResult arg32 = this.queryDao.doQuery(
				EncCommonHelper.getCurrentPageIndex(request),
				EncCommonHelper.getPageSize(request), arg27, arg30,
				(String[]) null, shortNameMap, retObjs, (String[]) null);
		mv.addObject("qr", arg32);
		mv.addAllObjects(parameters);
		List countList = this.queryDao.doQuery(arg27,
				(OrderParameterCarrier[]) null, new String[]{"h.status"},
				shortNameMap, new String[]{"h.status", "sum(h.smsNum)"},
				(String[]) null).getList();
		String sCount = "0";
		String fCount = "0";

		for (int i = 0; i < countList.size(); ++i) {
			Object[] objs = (Object[]) countList.get(i);
			if (objs != null) {
				if (objs[0].toString().equals("S")) {
					sCount = objs[1].toString();
				} else {
					fCount = objs[1].toString();
				}
			}
		}

		mv.addObject("sCount", sCount);
		mv.addObject("fCount", fCount);
		return mv;
	}

	public ModelAndView handleHistoryList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "SMS_HIST")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.historyListView);
			String year = request.getParameter("year");
			SimpleDateFormat sf = new SimpleDateFormat("yyyy");
			if (year == null || year.equals("")) {
				year = sf.format(new Date());
			}

			String hisObjName = "com.minierp.model.SmsCount";
			String hisObjShortName = "c";
			HashMap shortNameMap = new HashMap();
			String[] retObjs = new String[]{hisObjShortName};
			Vector qpVe = new Vector();
			shortNameMap.put(hisObjName, hisObjShortName);
			QueryParameterCarrier qp = new QueryParameterCarrier();
			qp.objectName = hisObjName;
			qp.propertyName = "year";
			qp.value = " c.year=" + year;
			qp.dataType = 6;
			qp.logicType = 10;
			qpVe.add(qp);
			QueryParameterCarrier[] qParas = new QueryParameterCarrier[qpVe
					.size()];

			for (int p1 = 0; p1 < qpVe.size(); ++p1) {
				qParas[p1] = (QueryParameterCarrier) qpVe.get(p1);
			}

			OrderParameterCarrier arg22 = new OrderParameterCarrier();
			arg22.objectName = hisObjName;
			arg22.propertyName = "stafferId";
			arg22.orderTarget = "asc";
			OrderParameterCarrier[] oParas = new OrderParameterCarrier[]{arg22};
			List list = this.queryDao.doQuery(qParas, oParas, (String[]) null,
					shortNameMap, retObjs, (String[]) null).getList();
			ArrayList arr = new ArrayList();
			String[] item = (String[]) null;
			String[] totalArr = new String[]{"0", "0", "0", "0", "0", "0", "0",
					"0", "0", "0", "0", "0", "0"};
			String stafferId = "";
			int itemTotal = 0;

			for (int i = 0; i < list.size(); ++i) {
				SmsCount c = (SmsCount) list.get(i);
				if (i != 0
						&& stafferId.equals(String.valueOf(c.getStafferId()))) {
					itemTotal += c.getAmount();
				} else {
					if (i != 0) {
						item[14] = String.valueOf(itemTotal);
						arr.add(item);
					}

					item = new String[]{"", "", "0", "0", "0", "0", "0", "0",
							"0", "0", "0", "0", "0", "0", "0"};
					item[0] = String.valueOf(c.getStafferId());
					item[1] = c.getStafferName();
					itemTotal = c.getAmount();
					stafferId = String.valueOf(c.getStafferId());
				}

				item[c.getMonth() + 1] = String.valueOf(c.getAmount());
				totalArr[c.getMonth() - 1] = String.valueOf(Integer
						.parseInt(totalArr[c.getMonth() - 1]) + c.getAmount());
				totalArr[12] = String.valueOf(Integer.parseInt(totalArr[12])
						+ c.getAmount());
			}

			if (item != null) {
				item[14] = String.valueOf(itemTotal);
				arr.add(item);
			}

			mv.addObject("year", new Integer(year));
			mv.addObject("lastYear", new Integer(sf.format(new Date())));
			mv.addObject("list", arr);
			mv.addObject("total", totalArr);
			return mv;
		}
	}

	public ModelAndView handleHistoryDetail(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.historyDetailView);
		String hisObjName = "com.minierp.model.SmsHistory";
		String hisObjShortName = "h";
		HashMap shortNameMap = new HashMap();
		String[] retObjs = new String[]{hisObjShortName};
		Vector qpVe = new Vector();
		HashMap parameters = new HashMap();
		shortNameMap.put(hisObjName, hisObjShortName);
		QueryParameterCarrier qp = new QueryParameterCarrier();
		String stafferId = request.getParameter("staffer_id");
		String stafferName = request.getParameter("staffer_name");
		qp.objectName = hisObjName;
		qp.propertyName = "createrId";
		qp.value = " h.createrId=" + stafferId;
		qp.dataType = 6;
		qp.logicType = 10;
		qpVe.add(qp);
		parameters.put("stafferId", stafferId);
		parameters.put("stafferName", stafferName);
		String searchType = request.getParameter("search_type");
		String startDate = request.getParameter("start_date");
		String endDate = request.getParameter("end_date");
		String status = request.getParameter("status");
		String recName = request.getParameter("rec_name");
		String recCode = request.getParameter("rec_code");
		String qParas;
		String p1;
		if (searchType != null && !searchType.equals("")) {
			if (searchType.equals("2")) {
				qp = new QueryParameterCarrier();
				qp.objectName = hisObjName;
				qp.propertyName = "createTime";
				qp.value = startDate;
				qp.dataType = 4;
				qp.logicType = 5;
				qpVe.add(qp);
				qp = new QueryParameterCarrier();
				qp.objectName = hisObjName;
				qp.propertyName = "createTime";
				qp.value = EncCommonHelper
						.getNextDateStr(endDate, "yyyy-MM-dd");
				qp.dataType = 4;
				qp.logicType = 4;
				qpVe.add(qp);
			} else {
				qParas = request.getParameter("cb_cre_date");
				p1 = request.getParameter("cb_status");
				String arg29 = request.getParameter("cb_rec_name");
				String arg32 = request.getParameter("cb_rec_code");
				if (qParas != null && qParas.equals("1")) {
					qp = new QueryParameterCarrier();
					qp.objectName = hisObjName;
					qp.propertyName = "createTime";
					qp.value = startDate;
					qp.dataType = 4;
					qp.logicType = 5;
					qpVe.add(qp);
					qp = new QueryParameterCarrier();
					qp.objectName = hisObjName;
					qp.propertyName = "createTime";
					qp.value = EncCommonHelper.getNextDateStr(endDate,
							"yyyy-MM-dd");
					qp.dataType = 4;
					qp.logicType = 4;
					qpVe.add(qp);
					parameters.put("cbCreDate", qParas);
				}

				if (p1 != null && p1.equals("1")) {
					if (!status.equalsIgnoreCase("A")) {
						qp = new QueryParameterCarrier();
						qp.objectName = hisObjName;
						qp.propertyName = "status";
						qp.value = status;
						qp.dataType = 1;
						qp.logicType = 1;
						qpVe.add(qp);
					}

					parameters.put("cbStatus", p1);
				}

				if (arg29 != null && arg29.equals("1")) {
					qp = new QueryParameterCarrier();
					qp.objectName = hisObjName;
					qp.propertyName = "receiverName";
					qp.value = recName;
					qp.dataType = 1;
					qp.logicType = 9;
					qpVe.add(qp);
					parameters.put("cbRecName", arg29);
				}

				if (arg32 != null && arg32.equals("1")) {
					qp = new QueryParameterCarrier();
					qp.objectName = hisObjName;
					qp.propertyName = "receiverCode";
					qp.value = recCode;
					qp.dataType = 1;
					qp.logicType = 9;
					qpVe.add(qp);
					parameters.put("cbRecCode", arg32);
				}
			}
		} else {
			searchType = "1";
			qParas = request.getParameter("year");
			p1 = request.getParameter("month");
			Calendar oParas = Calendar.getInstance();
			SimpleDateFormat qr = new SimpleDateFormat("yyyy-MM-dd");
			startDate = qParas + "-" + p1 + "-01";
			oParas.setTime(qr.parse(startDate));
			oParas.add(2, 1);
			oParas.add(5, -1);
			endDate = qr.format(oParas.getTime());
			qp = new QueryParameterCarrier();
			qp.objectName = hisObjName;
			qp.propertyName = "createTime";
			qp.value = startDate;
			qp.dataType = 4;
			qp.logicType = 5;
			qpVe.add(qp);
			qp = new QueryParameterCarrier();
			qp.objectName = hisObjName;
			qp.propertyName = "createTime";
			qp.value = EncCommonHelper.getNextDateStr(endDate, "yyyy-MM-dd");
			qp.dataType = 4;
			qp.logicType = 4;
			qpVe.add(qp);
			parameters.put("cbCreDate", "1");
			status = "S";
			qp = new QueryParameterCarrier();
			qp.objectName = hisObjName;
			qp.propertyName = "status";
			qp.value = status;
			qp.dataType = 1;
			qp.logicType = 1;
			qpVe.add(qp);
			parameters.put("cbStatus", "1");
		}

		parameters.put("searchType", searchType);
		parameters.put("startDate", startDate);
		parameters.put("endDate", endDate);
		parameters.put("status", status);
		parameters.put("recName", recName);
		parameters.put("recCode", recCode);
		QueryParameterCarrier[] arg27 = new QueryParameterCarrier[qpVe.size()];

		for (int arg28 = 0; arg28 < qpVe.size(); ++arg28) {
			arg27[arg28] = (QueryParameterCarrier) qpVe.get(arg28);
		}

		OrderParameterCarrier arg30 = new OrderParameterCarrier();
		arg30.objectName = hisObjName;
		arg30.propertyName = "id";
		arg30.orderTarget = "desc";
		OrderParameterCarrier[] arg31 = new OrderParameterCarrier[]{arg30};
		QueryResult arg33 = this.queryDao.doQuery(
				EncCommonHelper.getCurrentPageIndex(request),
				EncCommonHelper.getPageSize(request), arg27, arg31,
				(String[]) null, shortNameMap, retObjs, (String[]) null);
		mv.addObject("qr", arg33);
		mv.addAllObjects(parameters);
		List countList = this.queryDao.doQuery(arg27,
				(OrderParameterCarrier[]) null, new String[]{"h.status"},
				shortNameMap, new String[]{"h.status", "sum(h.smsNum)"},
				(String[]) null).getList();
		String sCount = "0";
		String fCount = "0";

		for (int i = 0; i < countList.size(); ++i) {
			Object[] objs = (Object[]) countList.get(i);
			if (objs != null) {
				if (objs[0].toString().equals("S")) {
					sCount = objs[1].toString();
				} else {
					fCount = objs[1].toString();
				}
			}
		}

		mv.addObject("sCount", sCount);
		mv.addObject("fCount", fCount);
		return mv;
	}

	public ModelAndView handleGetBalance(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String result = "ERROR";
		String smsCode = PageHelper.getSmsCode();
		if (smsCode != null && !smsCode.equals("")) {
			double out = PageHelper.getSmsBalance();
			if (out != -1.0D) {
				int pointNum = (new Double(out * 100.0D)).intValue();
				int smsNum = (new Double(out * 10.0D)).intValue();
				result = String.valueOf(pointNum) + ";" + smsNum;
			} else {
				result = "ERROR";
			}
		} else {
			result = "NO_SMS";
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ServletOutputStream out1 = response.getOutputStream();
		out1.write(result.getBytes());
		out1.flush();
		out1.close();
		return null;
	}

	public ModelAndView handleSubscribe(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "SMS_SUBS")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			List list = this.smsAlertDao.getList();
			String flag = request.getParameter("flag");
			ModelAndView mv;
			if (flag != null && flag.equals("1")) {
				for (int i = 0; i < list.size(); ++i) {
					SmsAlert sa = (SmsAlert) list.get(i);
					String status = request.getParameter("status" + sa.getId());
					if (status != null && status.equals("1")) {
						sa.setStatus("Y");
					} else {
						sa.setStatus("N");
					}

					if (sa.getPtType() != null && sa.getPtType().equals("TIME")) {
						sa.setPtHour(Integer.parseInt(request
								.getParameter("pt_hour" + sa.getId())));
						sa.setPtMinute(Integer.parseInt(request
								.getParameter("pt_minute" + sa.getId())));
					} else if (sa.getPtType() != null
							&& sa.getPtType().equals("CYCLE")) {
						sa.setAlertCycle(Integer.parseInt(request
								.getParameter("cycle" + sa.getId())));
					}

					if (sa.getUseCustom().equals("Y")) {
						sa.setCustomTxt(request.getParameter("custom_txt"
								+ sa.getId()));
					}

					if (sa.getCanSms().equals("Y")) {
						String smsFlag = request.getParameter("sms_flag"
								+ sa.getId());
						if (smsFlag != null && smsFlag.equals("1")) {
							sa.setSmsFlag("Y");
						} else {
							sa.setSmsFlag("N");
						}
					}

					this.stafferDao.update(sa);
				}

				EncCommonHelper.setAlertMessage(request, "保存成功！");
				mv = new ModelAndView(
						new RedirectView("/sms/SmsSubscribe.ihtm"));
			} else {
				mv = new ModelAndView(this.subscribeView);
				mv.addObject("list", list);
			}

			return mv;
		}
	}

	public ModelAndView handleReceiverList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.receiverListView);
		String alertId = request.getParameter("alert_id");
		mv.addObject("list",
				this.smsAlertDao.getReceiverList(Long.parseLong(alertId)));
		mv.addObject("smsAlert",
				this.smsAlertDao.get(SmsAlert.class, Long.parseLong(alertId)));
		return mv;
	}

	public ModelAndView handleReceiverAdd(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String alertId = request.getParameter("alert_id");
		String receiverId = request.getParameter("receiver_id");
		SmsAlertReceiver r = new SmsAlertReceiver();
		r.setAlertId(Long.parseLong(alertId));
		r.setId(this.idGenerator.getId(SmsAlertReceiver.class.getName()));
		r.setReceiverId(Long.parseLong(receiverId));
		this.smsAlertDao.save(r);
		EncCommonHelper.setAlertMessage(request, "增加成功！");
		return new ModelAndView(new RedirectView(
				"/sms/SmsReceiverList.ihtm?alert_id=" + alertId));
	}

	public ModelAndView handleReceiverDel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String alertId = request.getParameter("alert_id");
		String id = request.getParameter("id");
		this.smsAlertDao.deleteReceiver(Long.parseLong(id));
		EncCommonHelper.setAlertMessage(request, "删除成功！");
		return new ModelAndView(new RedirectView(
				"/sms/SmsReceiverList.ihtm?alert_id=" + alertId));
	}

	public String getSendFormView() {
		return this.sendFormView;
	}

	public void setSendFormView(String sendFormView) {
		this.sendFormView = sendFormView;
	}

	public StafferDao getStafferDao() {
		return this.stafferDao;
	}

	public void setStafferDao(StafferDao stafferDao) {
		this.stafferDao = stafferDao;
	}

	public EstablishmentLevelDao getEstLevelDao() {
		return this.estLevelDao;
	}

	public void setEstLevelDao(EstablishmentLevelDao estLevelDao) {
		this.estLevelDao = estLevelDao;
	}

	public EstablishmentTypeDao getEstTypeDao() {
		return this.estTypeDao;
	}

	public void setEstTypeDao(EstablishmentTypeDao estTypeDao) {
		this.estTypeDao = estTypeDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public SmsCountDao getSmsCountDao() {
		return this.smsCountDao;
	}

	public void setSmsCountDao(SmsCountDao smsCountDao) {
		this.smsCountDao = smsCountDao;
	}

	public SmsService getSmsService() {
		return this.smsService;
	}

	public void setSmsService(SmsService smsService) {
		this.smsService = smsService;
	}

	public String getMyHistoryListView() {
		return this.myHistoryListView;
	}

	public void setMyHistoryListView(String myHistoryListView) {
		this.myHistoryListView = myHistoryListView;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public String getHistoryDetailView() {
		return this.historyDetailView;
	}

	public void setHistoryDetailView(String historyDetailView) {
		this.historyDetailView = historyDetailView;
	}

	public String getHistoryListView() {
		return this.historyListView;
	}

	public void setHistoryListView(String historyListView) {
		this.historyListView = historyListView;
	}

	public String getSubscribeView() {
		return this.subscribeView;
	}

	public void setSubscribeView(String subscribeView) {
		this.subscribeView = subscribeView;
	}

	public SmsAlertDao getSmsAlertDao() {
		return this.smsAlertDao;
	}

	public void setSmsAlertDao(SmsAlertDao smsAlertDao) {
		this.smsAlertDao = smsAlertDao;
	}

	public String getReceiverListView() {
		return this.receiverListView;
	}

	public void setReceiverListView(String receiverListView) {
		this.receiverListView = receiverListView;
	}

	public EstablishmentContactDao getContactDao() {
		return this.contactDao;
	}

	public void setContactDao(EstablishmentContactDao contactDao) {
		this.contactDao = contactDao;
	}

	public String getSendFormMultiView() {
		return this.sendFormMultiView;
	}

	public void setSendFormMultiView(String sendFormMultiView) {
		this.sendFormMultiView = sendFormMultiView;
	}

	public EstablishmentCommunicateDao getEstablishmentCommunicateDao() {
		return this.establishmentCommunicateDao;
	}

	public void setEstablishmentCommunicateDao(
			EstablishmentCommunicateDao establishmentCommunicateDao) {
		this.establishmentCommunicateDao = establishmentCommunicateDao;
	}

	public PhoneBookDao getPhoneBookDao() {
		return this.phoneBookDao;
	}

	public void setPhoneBookDao(PhoneBookDao phoneBookDao) {
		this.phoneBookDao = phoneBookDao;
	}
}